import React, { useEffect } from "react";
// 通常情况下动态路由都是用在详情页上面
// useParams是路由提供的hooks，作用是拿到动态路由的参数
import { Link, Routes, Route, useParams } from "react-router-dom";

const Home = () => {
  return <div>这是首页</div>;
};
const About = () => {
  return <div>这是关于页</div>;
};

const Detail = () => {
  const params = useParams();
  useEffect(() => {
    // 就可以在这里去做数据请求了
    console.log(params.id);
  }, [params.id]);
  return <div>这是详情页</div>;
};

const App = () => {
  return (
    <>
      <h2>params动态路由</h2>
      <ul>
        <li>
          <Link to="/">首页</Link>
        </li>
        <li>
          <Link to="/about">关于页</Link>
        </li>
        <li>
          <Link to="/detail/123">详情页</Link>
        </li>
        <li>
          <Link to="/detail/456">详情页</Link>
        </li>
      </ul>

      <hr />

      <Routes>
        <Route path="/" element={<Home />}></Route>
        <Route path="/about" element={<About />}></Route>
        {/* 动态路由的做法和vue是一样的 */}
        <Route path="/detail/:id" element={<Detail />}></Route>
      </Routes>
    </>
  );
};

export default App;
